<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="tj">
        <i class="layui-icon layui-icon-engine"></i>
        推荐分类
    </button>
</script>


<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>
<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script type="text/html" id="user-img">
    <div class="user-info-head">
        <img src="{{d.icon}}" width="30px" height="30px" alt="">
    </div>
</script>
<script type="text/html" id="user-img2">
    <div class="user-info-head">
        <img src="{{d.icon_pc}}" width="30px" height="30px" alt="">
    </div>
</script>
<script type="text/html" id="user-img3">
    <div class="user-info-head">
        <img src="{{d.icon_pc_on}}" width="30px" height="30px" alt="">
    </div>
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dropdown = layui.dropdown;

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center'
                },
                {
                    title: '排序',
                    field: 'sort',
                    align: 'center',
                    edit:'text'
                },
                {
                    title: '分类名称(CN)',
                    field: 'name',
                    align: 'center'
                },
                {
                    title: '分类名称(EN)',
                    field: 'e_name',
                    align: 'center'
                },
                {
                    title: '图标',
                    field: 'icon',
                    align: 'center',
                    templet: '#user-img'
                },
                {
                    title: 'Pc未选中图标',
                    field: 'icon_pc',
                    align: 'center',
                    templet: '#user-img2'
                },
                {
                    title: 'Pc选中图标',
                    field: 'icon_pc',
                    align: 'center',
                    templet: '#user-img3'
                },
                {
                    title: '课程数',
                    field: 'course_total',
                    align: 'center'
                },
                {
                    title: '创建时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Category/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter'],
            done: function(res, curr, count) {
                for (var i = 0; i < res.data.length; i++) {

                    if (res.data[i].id == 42 || res.data[i].id == 1007) {
                        var datas = [{
                            title: '修改',
                            id: res.data[i].id,
                            type: 1,
                        }, {
                            title: '删除',
                            id: res.data[i].id,
                            type: 3
                        }];
                    } else {
                        var datas = [{
                            title: '修改',
                            id: res.data[i].id,
                            type: 1,
                        }, {
                            title: '课程',
                            id: res.data[i].id,
                            type: 2
                        }, {
                            title: '删除',
                            id: res.data[i].id,
                            type: 3
                        }]
                    }
                    dropdown.render({
                        elem: '#more_' + res.data[i].id,
                        data: datas,
                        id: '#more_' + res.data[i].id,
                        click: function(obj) {
                            if (obj.type == 1) {

                                layer.open({
                                    type: 2,
                                    title: '修改',
                                    shade: 0.1,
                                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                                    content: '/Category/edit?id='+obj.id
                                });

                            } else if(obj.type == 2) {
                                parent.layui.admin.addTab('category'+obj.id, obj.id+"选择课程", '/Category/course?id='+obj.id)
                            } else if(obj.type == 3) {
                                layer.confirm('确定要删除分类吗?', {
                                    icon: 3,
                                    title: '提示'
                                }, function(index) {
                                    layer.close(index);
                                    let loading = layer.load();
                                    $.ajax({
                                        url: "/Category/delete",
                                        data:{id:obj.id},
                                        dataType: 'json',
                                        type: 'post',
                                        success: function(result) {
                                            layer.close(loading);
                                            if (result.status == 0) {
                                                layer.msg(result.msg, {
                                                    icon: 1,
                                                    time: 1000
                                                }, function() {
                                                    table.reload('user-table');
                                                });
                                            } else {
                                                layer.msg(result.msg, {
                                                    icon: 2,
                                                    time: 1000
                                                });
                                            }
                                        }
                                    })
                                });
                            }
                        }
                    });
                }
            }
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });



        //监听单元格编辑
        table.on('edit(user-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
            $.post("{:url('sort')}", {id: data.id,sort:value}, function (result) {
                if(result.status == 0) {
                    table.reload("user-table");
                } else {
                    layer.msg(result.msg,{icon:2,time:1000});
                    table.reload("user-table");
                }
            });
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'tj') {
                window.tj();
            }
        });



        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Category/add'
            });
        }

        window.tj = function() {
            layer.open({
                type: 2,
                title: '推荐分类',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Category/edit?id=10001'
            });
        }



        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
